package
{
	import data.CaptionTxt;
	import data.Globals;
	
	import events.EventType;
	import events.GameDispatherEvent;
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.StageOrientationEvent;
	import flash.system.Capabilities;
	
	import interfaces.ITowerWar;
	
	import manager.LevelManange;
	import manager.MapConfig;
	
	import view.AlertView;
	import view.AnimationView;
	import view.CaptionView;
	import view.GameView;
	import view.LoginView;

	/**
	 *@author CrazyChen 
	 */	
	[SWF(frameRate="30",width="900",height="468")]
	public class TowerWar extends Sprite implements ITowerWar
	{
		private var gameView:GameView;
		
		private var loginView:LoginView;
		
		/**
		 * 是否是登录界面进入关卡 
		 */
		private var isLoginEnter:Boolean = true;
		
		public function TowerWar()
		{
			Globals.towerWar = this;
			if(stage){
				addToStage();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			}
		}
		
		protected function addToStage(event:Event=null):void
		{
//			stage.stageWidth = Capabilities.screenResolutionY;
//			stage.stageHeight = Capabilities.screenResolutionX;
			Globals.stage_width = this.stage.stageWidth;
			Globals.stage_height = this.stage.stageHeight;
//			this.stage.autoOrients = true;
			
//			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			loginView = new LoginView();
			this.addChild(loginView);
			GameDispatherEvent.getInstance();//初始化事件管理器
			this.addEventListener(Event.ENTER_FRAME,loop);
		}		
		
		public function startGame():void{
			gameUpLevel();
		}
		
		public function returnLogin():void{
			gameView.resetView();
			this.removeChild(gameView);
			this.addChild(loginView);
		}
		
		public function gameUpLevel(isLoginEnter:Boolean = true):void{
			if(!isLoginEnter){
				loginView.unlockNext();
				gameView.showAlert("成功狙击日军一个小队，其他阵地需要支援，是否前去支援！！！",alertCallback,true,true);
			}else{
				if(LevelManange.getInstance().curLevel == 1){
					var ca:CaptionView = new CaptionView();
					this.addChild(ca);
					ca.show(CaptionTxt.firstlogin,loaderConfig);
				}else{
					loaderConfig();
				}
			}
		}
		
		/**
		 *通关 
		 */
		public function gameWin():void{
			gameView.showAlert("1945年8月15日，日本天皇宣布无条件投降！",returnLogin,true,false);
		}
		
		private function loaderConfig():void{
			MapConfig.getInstance().loaderComplete = mapComplete;
			MapConfig.getInstance().loadXml(LevelManange.getInstance().curLevel);
		}
		
		private function mapComplete():void
		{
			if(!gameView)gameView = new GameView();
			this.addChild(gameView);
			if(isLoginEnter){			
				gameView.playAnimation(AnimationView.COUNT_DOWN,gameView.start);
			}else{
				gameView.playAnimation(AnimationView.NEXT_LEVEL,function():void{
					gameView.playAnimation(AnimationView.COUNT_DOWN,gameView.start);
				});
			}
		}
		
		private function alertCallback(shouse:String):void{
			if(shouse == AlertView.YES){
				loaderConfig();
			}else{
				returnLogin();
				isLoginEnter = true;
			}
		}
		
		private function loop(event:Event):void
		{
			GameDispatherEvent.dispatchEvent(EventType.LOOP);
		}
		
	}
}